package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entry.YddAdvimg;
import com.itheima.ydd.entry.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@AllArgsConstructor
@Slf4j
public class YddAdvimgController {
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddAdvposService yddAdvposService;
    //分页查询
    @GetMapping("/list")
    public R page(String advimgdesc, String advimgpos, int page, int limit){
        //分页构造器
        Page<YddAdvimg> pageInfo = new Page<>(page,limit);
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc,advimgdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos,advimgpos);
        //添加排序条件
        queryWrapper.orderByDesc(YddAdvimg::getAdvimgpos);
        yddAdvimgService.page(pageInfo,queryWrapper);


        Page<YddAdvimgDto> advimgDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,advimgDtoPage,"records");
        List<YddAdvimg> records = pageInfo.getRecords();
        List<YddAdvimgDto> yddAdvimgs =  new ArrayList<>();

        yddAdvimgs=records.stream().map(item ->{
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item, yddAdvimgDto);
            //给advposnum赋值
            YddAdvpos advpos = yddAdvposService.getById(item.getAdvimgpos());
            if (advpos != null){
                    yddAdvimgDto.setAdvposname(advpos.getAdvposname());
                }
            return yddAdvimgDto;
        }).collect(Collectors.toList());
        advimgDtoPage.setRecords(yddAdvimgs);

        Map map = new HashMap<String,Object>();

        map.put("count",advimgDtoPage.countId());
        map.put("totalPages",advimgDtoPage.getTotal());
        map.put("pageSize",advimgDtoPage.getMaxLimit());
        map.put("currentPage",advimgDtoPage.getCurrent());
        map.put("data",advimgDtoPage.getRecords());

        return R.success(map);
    }

    //回显数据
    @GetMapping("/rowInfo")
    public R rowInfo(Integer id){
        YddAdvimg adv = yddAdvimgService.getById(id);

        return R.success(adv);
    }
    //修改
    @PostMapping("update")
    public R update(@RequestBody YddAdvimg yddAdvimgs){
        yddAdvimgService.updateById(yddAdvimgs);
        return R.success("修改成功");
    }

    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg){

        yddAdvimgService.save(yddAdvimg);
        return R.success("添加成功");
    }

    //删除
    @GetMapping("/del")
    public R<String> delete(Long id){
        System.out.println(".............");
        System.out.println(".............");
        yddAdvimgService.removeById(id);
        return R.success("套餐删除数据成功");
    }

}
